package com.lucy.flow.domain.entity;

import com.lucy.common.core.BaseEntity;
import com.lucy.flow.constant.FlowWorkStatusConst;
import com.lucy.flow.converter.FlowModelConverter;
import com.lucy.flow.converter.FlowParamConverter;
import com.lucy.flow.domain.dto.FlowParam;
import com.lucy.flow.model.FlowModel;
import com.lucy.flow.model.StartModel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
@Table(name = "flow_work")
@ApiModel(value = "流程实例",description = "")
public class FlowWork extends BaseEntity implements Serializable {
    public FlowWork(FlowDefine flowDefine,String operator,FlowModel flowModel){
        this.flowName = flowDefine.getFlowName();
        this.flowDefineId = flowDefine.getId();
        this.flowDefineCode = flowDefine.getFlowDefineCode();
        this.parentId = null;
        this.flowModel = flowModel;
        this.initiator = operator;
        StartModel startModel = flowModel.getStartModel();
        this.currentNodeId = startModel.getId();
        this.currentNodeName = startModel.getName();
        this.nextNodeId = startModel.getNextNodeId();
        // 进行中
        this.flowWorkStatus = FlowWorkStatusConst.IN_PROGRESS;
        // 正常
        this.status = 1;
    }
    /**
     * 可以使用 JPA 级联映射
     */
    @ApiModelProperty("流程名称")
    @Column(name = "flow_name")
    String flowName;

    @ApiModelProperty("流程定义ID")
    @Column(name = "flow_define_id")
    String flowDefineId;

    @ApiModelProperty("流程定义编码")
    @Column(name = "flow_define_code")
    String flowDefineCode;

    // 一般使用不到
    @ApiModelProperty("父级流程ID")
    @Column(name = "parent_id")
    String parentId;

    @Convert(converter = FlowModelConverter.class)
    @ApiModelProperty(value = "流程模型",notes = "JSON字符串")
    @Column(name = "flow_model",length = 5096)
    FlowModel flowModel;

    @ApiModelProperty("流程发起者ID")
    @Column(name = "initiator")
    String initiator;

    @ApiModelProperty("当前节点ID")
    @Column(name = "current_node_id")
    String currentNodeId;

    @ApiModelProperty("当前节点名称")
    @Column(name = "current_node_name")
    String currentNodeName;

    @ApiModelProperty("下一节点ID")
    @Column(name = "next_node_id")
    String nextNodeId;
    /**
     * 流程参数， 全流程可访问
     */
    @Convert(converter = FlowParamConverter.class)
    @ApiModelProperty(value = "流程参数",name = "")
    @Column(name = "flow_param",length = 5096)
    FlowParam flowParam;

    @ApiModelProperty(value = "流程实例状态",name = "")
    @Column(name = "flow_work_status")
    String flowWorkStatus;


    @ApiModelProperty(value = "实例状态",notes = "1正常 0被挂起 2被锁定")
    @Column(name = "status")
    Integer status;


    /**
     * 业务类型
     */
    @ApiModelProperty("业务类型")
    @Column(name = "biz_type")
    String bizType;

    /**
     * 关联的业务ID
     */
    @ApiModelProperty("业务ID")
    @Column(name = "biz_id")
    String bizId;



}
